package com.game;

import android.content.Intent;
import android.database.Cursor;
import android.widget.Button;
import android.widget.EditText;

public class EditStepItemActivity extends EditTableActivity {

	private EditText stepGameIdEditText;
	private EditText choiceFromEditText;
	private EditText choiceToEditText;
	
	@Override
	protected void initalize() {
		setContentView(R.layout.editstepitem);

		SubmitButton = (Button) findViewById(R.id.si_submitButton);
		SubmitButton.setOnClickListener(this);
		ViewButton = (Button) findViewById(R.id.si_viewStepItemButton);
		ViewButton.setOnClickListener(this);
		HomeButton = (Button) findViewById(R.id.si_homeButton);
		HomeButton.setOnClickListener(this);
		EditButton = (Button) findViewById(R.id.si_editStepItemButton);
		EditButton.setOnClickListener(this);
		
		stepGameIdEditText = (EditText)findViewById(R.id.si_gameIdEditText);
		choiceFromEditText = (EditText)findViewById(R.id.si_choiceFromEditText);
		choiceToEditText = (EditText)findViewById(R.id.si_choiceToEditText);

		ViewClass = ViewStepItemActivity.class;
	}
	
    protected void save(){
    	String gameIdStr = stepGameIdEditText.getText().toString();
    	String choiceFrom = choiceFromEditText.getText().toString();
    	String choiceTo = choiceToEditText.getText().toString();
    	int gameId = Integer.parseInt(gameIdStr);
    	
    	StepItem newStep = new StepItem(gameId,choiceFrom,choiceTo);
    	myDBAdapter.insertStepItemData(newStep);
    	
    	
        Intent intent = new Intent(this, ViewStepItemActivity.class);
        startActivity(intent);
       	view();
    }
    
	@Override
	protected void edit() {
		startActivityForResult(new Intent(this,ListStepItemActivity.class),0);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{  
		super.onActivityResult(requestCode, resultCode, intent);
		switch(requestCode)
		{
		case 0:
			if (resultCode == RESULT_OK && intent.hasExtra("ItemId"))
			{
			int _id = intent.getIntExtra("ItemId", -1);
			Cursor cursor = myDBAdapter.getStepItemByIdCursor(_id);
			cursor.moveToFirst();
			stepGameIdEditText.setText(cursor.getString(cursor.getColumnIndex("game_id")));
			choiceFromEditText.setText(cursor.getString(cursor.getColumnIndex("choice_from")));
			choiceToEditText.setText(cursor.getString(cursor.getColumnIndex("choice_to")));
			}
			break;
		}
	}
	
	@Override
	public void onStart(){
		super.onResume();
		stepGameIdEditText.setText("");
		choiceFromEditText.setText("");
		choiceToEditText.setText("");
	}
}
